﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using PPClasses;

namespace PortalPlan.Solvers
{
    class IPRunner : Solver
    {
        private Process IPProcess;

        public IPRunner(List<Portal> points) : base(points)
        {
        }

        public override String StatusText()
        {
            if (!CompletelyDone)
            {
                return "Integer programming solver running...";
            }
            else
            {
                return base.StatusText();
            }
        }

        public override void GoGoGo()
        {
            String from = Path.GetTempPath() + Path.DirectorySeparatorChar + "problem.pplan";
            String to = Path.GetTempPath() + Path.DirectorySeparatorChar + "solution.ppairs";

            Problem p = new Problem() {ThePoints = this.PortalPoints};
            p.SaveTo(from);

            string basepath = (new FileInfo(Application.ExecutablePath)).DirectoryName + Path.DirectorySeparatorChar;
#if DEBUG
            string debrel="Debug";
#else
            string debrel="Release";
#endif
            string[] runoptions = new[] { basepath + "PortalPlan-IP.exe",
                basepath + @"..\..\..\..\PortalPlan-IP\bin\"+debrel+@"\PortalPlan-IP.exe"};

            string run = null;

            foreach (var runopt in runoptions)
            {
                if (File.Exists(runopt))
                {
                    run = "\"" + runopt + "\"";
                    break;
                }
            }
            if (string.IsNullOrEmpty(run))
            {
                MessageBox.Show("Unable to find PortalPlan-IP.exe", "Portal PLanner", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            String args = "\"" + from + "\" \"" + to + "\"";
            ProcessStartInfo psi = new ProcessStartInfo(run, args)
                {
                    WindowStyle = ProcessWindowStyle.Hidden
                };

            IPProcess = Process.Start(psi);
            if (IPProcess == null)
            {
                CompletelyDone = true;
                return;
            }

            bool ok = true;
            while (!IPProcess.HasExited)
            {
                Thread.Sleep(250);
                if (Stop)
                {
                    IPProcess.Kill();
                    ok = false;
                    break;
                }
            }

            if (ok && IPProcess.ExitCode == 0)
            {
                List<Pair> pairs = Solver.LoadPairsFrom(to);
                EvaluateSolution(pairs);
            }

            File.Delete(from);
            File.Delete(to);
            CompletelyDone = true;
            PercentDone = 100;
        }

        public override bool CanThread
        {
            get { return false; }
        }
    }
}
